package me.lc.hammerheads.layer;

import me.lc.hammerheads.core.GameManager;
import me.lc.hammerheads.core.GameMap;
import me.lc.hammerheads.core.GameSystem;
import me.lc.hammerheads.sprite.Hammer;
import me.lc.hammerheads.sprite.HeartBar;
import me.lc.hammerheads.sprite.LoseShowBlock;
import me.lc.hammerheads.sprite.PauseShopBlock;
import me.lc.hammerheads.sprite.StageShower;
import me.lc.hammerheads.sprite.ToolBar;
import me.lc.hammerheads.sprite.WinShowBlock;
import me.lc.hammerheads.util.AudioHelper;
import android.view.MotionEvent;

import com.wiyun.engine.actions.IntervalAction;
import com.wiyun.engine.actions.ScaleTo;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.types.WYPoint;
import com.wiyun.engine.utils.ResolutionIndependent;

public class GameLayer extends Layer {

	public Hammer hammer;
	public HeartBar heartBar;
	public StageShower stageShower;
	public ToolBar toolbar;
	public GameBgLayer bgLayer;

	public LoseShowBlock loseBlock;
	public WinShowBlock winBlock;
	public PauseShopBlock pauseBlock;

	public GameLayer() {
		this.setTouchEnabled(true);
		hammer = new Hammer();
		heartBar = new HeartBar();
		heartBar.setPosition(GameSystem.Screen_Width / 2.0f,
				GameSystem.Screen_Height - GameSystem.tex_heartBg.getHeight()
						/ 2.0f);
		stageShower = new StageShower();
		stageShower.setPosition(GameSystem.Screen_Width / 2.0f,
				GameSystem.Screen_Height - GameSystem.tex_heartBg.getHeight()
						- ResolutionIndependent.resolveDp(10));
		toolbar = new ToolBar();
		loseBlock = new LoseShowBlock();
		loseBlock.setPosition(GameSystem.Screen_Width / 2.0f,
				GameSystem.Screen_Height / 2.0f);
		winBlock = new WinShowBlock();
		winBlock.setPosition(GameSystem.Screen_Width / 2.0f,
				GameSystem.Screen_Height / 2.0f);
		pauseBlock = new PauseShopBlock();
		pauseBlock.setPosition(GameSystem.Screen_Width / 2.0f,
				GameSystem.Screen_Height / 2.0f);
		initSprites();
	}

	public void initSprites() {
		this.removeAllChildren(true);

		if (hammer != null) {
			hammer.stopAllActions(true);
			hammer.removeAllChildren(true);
			hammer.cancelTimer();
			hammer = null;
		}
		hammer = new Hammer();
		this.addChild(hammer, 3);

		heartBar.updateHeart(5);
		this.addChild(heartBar, 4);
		this.addChild(stageShower, 5);
		this.addChild(toolbar, 5);
	}

	@Override
	public boolean wyTouchesBegan(MotionEvent event) {
		if (!GameManager.instance.isPaused) {
			WYPoint point = this.convertTouchToNodeSpace(event);

			if (point.y < ResolutionIndependent.resolveDp(43)) {
				// 道具栏
				toolbar.processTouch(point);
			} else if (point.y <= GameSystem.Screen_Height) {

				WYPoint headPoint = GameMap.instance.testHit(point);
				if (headPoint != null) {
					point = headPoint;
				}
				hammer.knock(point);
			}
		}
		return true;
	}

	public void showLose() {
		removeChild(pauseBlock, true);
		addChild(loseBlock, 6);
		IntervalAction showAnimation = (IntervalAction) ScaleTo.make(0.5f,
				0.5f, 1.0f).autoRelease();
		loseBlock.runAction(showAnimation);
		AudioHelper.lose();
	}

	public void showWin() {
		removeChild(pauseBlock, true);
		addChild(winBlock, 6);
		IntervalAction showAnimation = (IntervalAction) ScaleTo.make(0.5f,
				0.5f, 1.0f).autoRelease();
		winBlock.runAction(showAnimation);
		winBlock.showScore();
	}

	public void pauseGame() {
		addChild(pauseBlock, 6);
		IntervalAction showAnimation = (IntervalAction) ScaleTo.make(0.3f,
				0.5f, 1.0f).autoRelease();
		pauseBlock.runAction(showAnimation);
		pauseBlock.show();

	}

	public void resumeGame() {
		removeChild(pauseBlock, true);
	}

}
